#!/usr/bin/python3
# -*- coding: utf8 -*-
# Date   : 2021/07/21
# Author : Kwan
# Email  : allenxian@live.com
# Blog   : https://my.oschina.net/u/4173440

from flask import Blueprint, render_template, redirect, url_for, request, json
from flask_login import login_required,current_user
from project.define import manager_required, app_logger
from .page import Messager


# 车场管理
carpark = Blueprint(name='carpark', import_name='project', url_prefix='/carpark', template_folder='routes/carpark/html')
# 页面消息管理器
PageMessager = Messager()


@carpark.route('/')
@login_required
@manager_required
def default():
    """
    重定向
    """
    return redirect(url_for('carpark.setting'))


@carpark.route('/setting', methods=['POST', 'GET'])
@login_required
@manager_required
def setting():
    """
    车场设置
    """
    view = "setting"
    if request.method == 'POST':
        try:
            # 检查数据类型
            p_data = request.get_json()
            if not isinstance(p_data, dict):
                p_data = json.loads(p_data)
            p_data['View'] = view
            app_logger.debug(f"用户：{current_user.LoginName } 接口：carpark.{view} 数据：{p_data}")
            return PageMessager.post_data_handler(**p_data)
        except Exception as e:
            app_logger.error(f"用户：{current_user.LoginName } 接口：carpark.{view} 错误：{e}")
        return PageMessager.error('處理錯誤，請聯係管理員')
    return render_template('carpark_setting.html', **PageMessager.setting_page_data())


@carpark.route('/place', methods=['POST', 'GET'])
@login_required
@manager_required
def place():
    """
    车位设置
    """
    view = "place"
    if request.method == 'POST':
        try:
            # 检查数据类型
            p_data = request.get_json()
            if not isinstance(p_data, dict):
                p_data = json.loads(p_data)
            p_data['View'] = view
            app_logger.debug(f"用户：{current_user.LoginName } 接口：carpark.{view} 数据：{p_data}")
            return PageMessager.post_data_handler(**p_data)
        except Exception as e:
            app_logger.error(f"用户：{current_user.LoginName } 接口：carpark.{view} 错误：{e}")
        return PageMessager.error('處理錯誤，請聯係管理員')
    return render_template('carpark_place.html', **PageMessager.place_page_data())


@carpark.route('/user', methods=['POST', 'GET'])
@login_required
@manager_required
def user():
    """
    收费员
    """
    view = "user"
    if request.method == 'POST':
        try:
            # 检查数据类型
            p_data = request.get_json()
            if not isinstance(p_data, dict):
                p_data = json.loads(p_data)
            p_data['View'] = view
            app_logger.debug(f"用户：{current_user.LoginName } 接口：carpark.{view} 数据：{p_data}")
            return PageMessager.post_data_handler(**p_data)
        except Exception as e:
            app_logger.error(f"用户：{current_user.LoginName } 接口：carpark.{view} 错误：{e}")
        return PageMessager.error('處理錯誤，請聯係管理員')
    return render_template('carpark_user.html', **PageMessager.user_page_data())


@carpark.route('/rule', methods=['POST', 'GET'])
@login_required
@manager_required
def rule():
    """
    收费规则
    """
    view = "rule"
    if request.method == 'POST':
        try:
            # 检查数据类型
            p_data = request.get_json()
            if not isinstance(p_data, dict):
                p_data = json.loads(p_data)
            p_data['View'] = view
            app_logger.debug(f"用户：{current_user.LoginName } 接口：carpark.{view} 数据：{p_data}")
            return PageMessager.post_data_handler(**p_data)
        except Exception as e:
            app_logger.error(f"用户：{current_user.LoginName } 接口：carpark.{view} 错误：{e}")
        return PageMessager.error('處理錯誤，請聯係管理員')
    return render_template('carpark_rule.html', **PageMessager.rule_page_data())
